import {
  deleteDeviceModel,
  getDeviceModelId,
  postDeviceModel,
  putDeviceModel,
  putDeviceModelName
} from '@/base/api/DeviceModelApi';
import {deepAssign} from '@/base/util/Objects';
import {getDeviceSort} from '@/base/api/DeviceSortApi';
import {Page} from '@/base/Page';
import {getCompanyList} from '@/base/api/CompanyApi';
import {Company} from '@/base/model/Company';
import {DeviceSort} from '@/base/model/DeviceSort';
import {DeviceModel, DeviceModelName} from '@/base/model/DeviceModel';

export class FormBloc {
  device: any = [];
  model: any = {
    id: 0 || undefined,
    companyId: 0,
    internalModel:'',
    isFirmware: true,
    isSelfMotion: true,
    isSoftware: true,
    modelIcon: '',
    modelName: '',
    configure: '',
    sortId: 0,
    poster: '',
    createTime: '',
    updateTime: '',
    isHide: 0,
    bletype:1,
    channel:1,
  };
  loading = false;
  fileList: Array<any> = []
  posterFileList: Array<any> = []
  num: number = 0;
  sortList: any = [];
  companyList: any = []
  sortId: number = 0;
  modelId: number = 0;
  isDetail = false;
  company: Page<Company> = new Page<Company>([], 0, 1, 10, 0, []);
  configureList: any = ['低配','中配','高配'];
  bletypeList: any = [{//1天猫精灵 2思必驰 3普通蓝牙
    id:1,title:'天猫精灵'
  },{
    id:2,title:'思必驰'
  },{
    id:3,title:'普通蓝牙'
  }];
  channelList:any = [{title:'线下',value:1},{title:'线上',value:2}]

  getAllSort(pageIndex: number, pageSize: number, sortDirection: string) {
    this.loading = true;
    let that = this;
    return getDeviceSort({
      pageIndex,
      pageSize,
      sortDirection
    }).then((res) => {
      this.sortList = res.data.items.map(function (item) {
        var obj = {id: 0, sortName: ''};
        // @ts-ignore
        obj.id = item.id;
        obj.sortName = item.sortName;
        return obj;
      });
      return this.sortList;
    }).finally(() => {
      this.loading = false;
    });
  }

  getCompanyList(pageIndex: number,
                 pageSize: number,
                 sortDirection: string) {
    return getCompanyList({pageIndex, pageSize, sortDirection, name: '', createBegin: ''})
      .then((res) => {
        deepAssign(this.company, res.data)
        this.companyList = res.data.items.map(item => {
          let obj = {companyId: 0, companyName: ''}
          obj.companyId = item.id;
          obj.companyName = item.name
          return obj
        })
        return res;
      }).finally(() => {
        this.loading = false;
      });
  }

  addModel(model) {
    this.loading = true;
    return postDeviceModel({
      companyId: model.companyId,
      channel:model.channel,
      internalModel:model.internalModel,
      isFirmware: model.isFirmware,
      isSelfMotion: model.isSelfMotion,
      isSoftware: model.isSoftware,
      modelIcon: model.modelIcon,
      modelName: model.modelName,
      sortId: model.sortId,
      poster: model.poster,
      configure: model.configure,
      bletype:model.bletype,
     
    }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  getModel(id: number) {
    this.loading = true;
    return getDeviceModelId({id}).then((res) => {
      this.model = res.data;
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  deviceModelName: Array<any> = []
  putDeviceModelGather() {
    return putDeviceModelName().then((res) => {
      this.deviceModelName = res.data
    })
  }

  editModel(model: any) {
    console.log(model,213)
    return putDeviceModel({
      id: model.id,
      companyId: model.companyId,
      channel:model.channel,
      internalModel:model.internalModel,
      isFirmware: model.isFirmware,
      isSelfMotion: model.isSelfMotion,
      isSoftware: model.isSoftware,
      modelIcon: model.modelIcon,
      modelName: model.modelName,
      sortId: model.sortId,
      isHide: model.isHide,
      poster: model.poster,
      configure: model.configure,
      bletype:model.bletype
    }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

}
